package com.rran.study.chat.ops.exception;

import com.rran.study.chat.common.exception.BusinessException;
import com.rran.study.chat.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yy
 * @Type GlobalExceptionHandler.java
 * @Desc
 * @date 2020/8/18
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 拦截业务异常
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Response businessException(BusinessException e) {
        log.info("业务异常:", e.getErrorMsg(),e);
        return Response.error(e);
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Response serverError(Exception e) {
        log.error("运行时异常:", e);
        return Response.error();
    }
}
